<template>
	<view class="register">

		<view class="content">
			<!-- 头部logo -->
			<view class="header">
				<image :src="logoImage"></image>
			</view>
			<!-- 主体 -->
			<view class="main">
				<wInput v-model="username" type="text" maxlength="11" placeholder="用户名/账号"></wInput>
				<wInput v-model="password" type="password" maxlength="11" placeholder="登录密码" isShowPass></wInput>
				<wInput v-model="password1" type="password" maxlength="11" placeholder="确认密码" isShowPass></wInput>

			</view>

			<wButton class="wbutton" text="注 册" :rotate="isRotate" bgColor="#2acaf6" @click.native="startReg()">
			</wButton>

			<!-- 底部信息 -->
			<view class="footer">
				<text @tap="isShowAgree" class="cuIcon" :class="showAgree?'cuIcon-radiobox':'cuIcon-round'"> 同意</text>
				<!-- 协议地址 -->
				<navigator url="/pages/user/agreement" open-type="navigate">《协议》</navigator>
			</view>
		</view>
	</view>
</template>

<script>
	let _this = {}
	import wInput from '../../components/watch-login/watch-input.vue' //input
	import wButton from '../../components/watch-login/watch-button.vue' //button

	export default {
		data() {
			return {
				//logo图片 base64
				logoImage: '',
				username: '', // 用户名/账号
				password: '', //密码
				password1: '', //密码
				showAgree: true, //协议是否选择
				isRotate: false, //是否加载旋转
			}
		},
		components: {
			wInput,
			wButton,
		},
		mounted() {
			_this = this;
		},
		methods: {
			isShowAgree() {
				//是否选择协议
				_this.showAgree = !_this.showAgree;
			},

			startReg() {
				//注册
				if (_this.isRotate) {
					//判断是否加载中，避免重复点击请求
					return false;
				}
				if (_this.showAgree == false) {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '请先同意《协议》'
					});
					return false;
				}
				if (_this.password.length < 6) {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '密码不正确'
					});
					return false;
				}
				if (_this.password != _this.password1) {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '密码不一致'
					});
					return false;
				}
				const data = {
					username: _this.username,
					password: _this.password,
					nickname: _this.username,
					collect: [],
					cart: [],
					address:[],
					avatar: "https://gd3.alicdn.com/imgextra/i4/479184430/O1CN01nCpuLc1iaz4bcSN17_!!479184430.jpg_400x400.jpg"
				}
				_this.isRotate = true
				uniCloud.callFunction({
					name: 'user-center',
					data: {
						action: 'register',
						params: data
					},
					success(e) {
						console.log("注册", e);

						if (e.result.code === 0) {
							_this.isRotate = false
							uni.showToast({
								title: '注册成功'
							});
							uni.setStorageSync('uni_id_token', e.result.token)
							uni.setStorageSync('username', e.result.username)
							uni.setStorageSync('nickname', e.result.userInfo.nickname)
							uni.reLaunch({
								url: '../index/index',
							});
						} else {
							uni.showModal({
								content: JSON.stringify(e.result),
								showCancel: false
							})
						}
					},
					fail(e) {
						uni.showModal({
							content: JSON.stringify(e),
							showCancel: false
						})
					}
				})
				// setTimeout(function(){
				// },3000)
			}
		}
	}
</script>

<style>
	@import url("../../components/watch-login/css/icon.css");
	@import url("./css/main.css");
</style>
